<?php
class AdService extends BaseDbService
{
	public function __construct()
	{
		$this->dao	= new AdDao();
	}
	
	public function add($data)
	{
		$id	= parent::add($data);
		$this->updateCache();
		return $id;
	}
	
	public function update($id,$data)
	{
		$ret	= parent::update($id,$data);
		$this->updateCache();
		return $ret;
	}
	
	public function del($id)
	{
		$ret	= parent::del($id);
		$this->updateCache();
		return $ret;
	}
	
	private function updateCache()
	{
		$condition['orderby'] 	= 'orderby DESC,id ASC';
		$list					= $this->dao->fetchRows($condition);
		
		$cache 	= new LtCache();
		$cache->init();
		$ch 	= $cache->getTDG(C('LtConfig')->get('db.table.prefix')."ad");
		$tmp	= $ch->get('ad_list');
		if($tmp!==false)
		{
			$ch->update('ad_list',$list);
		}
		else
		{
			$ch->add('ad_list',$list);
		}
	}
	
	public function getAdList($cache=true,$groupKeys=array())
	{
		if($cache)
		{
			$cache 	= new LtCache();
			$cache->init();
			$ch 	= $cache->getTDG(C('LtConfig')->get('db.table.prefix')."ad");
			$tmp	= $ch->get('ad_list');
		}
		else
		{
			$condition['orderby'] 	= 'orderby DESC,id ASC';
			$tmp	= $this->dao->fetchRows($condition);
		}
		if(!empty($groupKeys))
		{
			$cf				= C('CommonFunction');
			$tmp			= $cf->groupData($tmp,$groupKeys,true);
		}
		return $tmp;
	}
}